﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;
using Kgb.Model.Models;
using Kgb.Web2.Util;
using System.Net.Http;
using System.Web.Http.Routing;

namespace Kgb.Web2.Repository
{
    public class LobbyRepository
    {
        private string restService = ConfigurationManager.AppSettings["restService"];
        private RequestMethod requestMethod = new RequestMethod();
        ISerialization _serializer;

        public LobbyRepository(ISerialization serializer)
        {
            _serializer = serializer;
        }

        public List<Room> GetRooms(int id, int page, int take)
        {
            var responseStream = requestMethod
                                .GetResponseStream(requestMethod
                                                   .getRequest("GET"
                                                              , "application/json"
                                                              , string.Format("{0}/lobbies/rooms/1?page={1}&take={2}", restService, page.ToString(),take.ToString())
                                                              )
                                                   .GetResponse()
                                                  );

            var rooms = _serializer.DeSerialize(responseStream) as Newtonsoft.Json.Linq.JArray;

            var ret = from r in rooms
                      select new Room
                      {
                          Id = (int)r["Id"]
                      ,   LobbyId = (int?)r["LobbyId"]
                      ,   Name = r["Name"].ToString()
                      ,   State = r["State"].ToString()
                      };

            return ret.ToList();
        }
    }
}